package com.chalk.util;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.chalk.exception.ColumnNotEmptyException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Component
public class MpUtil<T> {


    public EntityWrapper<T> getWrapper() {
        EntityWrapper<T> wrapper = new EntityWrapper<T>();
        wrapper.eq("is_delete", "0");
        return wrapper;
    }

    public Page<T> getPage(Map<String, Object> map) {
        Page<T> page = new Page<T>(MapUtil.getPage(map), MapUtil.getSize(map));
        return page;
    }


    public List<String> getDesc(String... column) throws ColumnNotEmptyException {
        List<String> orderBy = new ArrayList<String>();
        checkColumn(column);
        for (String s : column) {
            orderBy.add(s);
        }
        return orderBy;


    }

    public List<String> getAsc(String... column) throws ColumnNotEmptyException {
        List<String> orderBy = new ArrayList<String>();
        checkColumn(column);
        for (String s : column) {
            orderBy.add(s);
        }
        return orderBy;

    }

    private void checkColumn(String... column) throws ColumnNotEmptyException {
        if (column.length == 0) {
            throw new ColumnNotEmptyException("column is not empty!!!");
        }
        for (String s : column) {
            if (StringUtils.isBlank(s)) {
                throw new ColumnNotEmptyException("column is not empty!!!");
            }
        }
    }
}
